﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Easy.Constant;
using Easy.Extend;
using Easy.MetaData;
using Easy.Web.CMS;
using Easy.Web.CMS.MetaData;
using Easy.Web.CMS.Widget;
using Microsoft.Practices.ServiceLocation;

namespace Easy.CMS.List.Models
{
    [DataConfigure(typeof(ListTops_PopModal_WidgeMetaData)), Serializable]
    public class ListTops_PopModal_Widget : WidgetBase
    {
        /// <summary>
        /// 绑定编辑表单的AppId
        /// </summary>
        public string AppId { get; set; }

        public string ListId { get; set; }

        public string FormCode { get; set; }

        public string FormId { get; set; }

        public string ListCode { get; set; }

        /// <summary>
        /// 列表Title 字段截取长度
        /// </summary>
        public int? ItemLength { get; set; }

        public int? Tops { get; set; }

        public string DetailPageUrl { get; set; }

        public string Width { get; set; }

        public string Height { get; set; }
    }

    class ListTops_PopModal_WidgeMetaData : WidgetMetaData<ListTops_PopModal_Widget>
    {
        protected override void ViewConfigure()
        {
            base.ViewConfigure();
            ViewConfig(m => m.Width).AsTextBox().Order(NextOrder());
            ViewConfig(m => m.Height).AsTextBox().Order(NextOrder());

            ViewConfig(m => m.Tops).AsTextBox().Order(NextOrder()).RegularExpression(RegularExpression.PositiveIntegers);

            ViewConfig(m => m.MoreLink).AsTextBox().Order(NextOrder()).AddClass("select").AddProperty("data-url", Urls.SelectPage);
            ViewConfig(m => m.DetailPageUrl).AsTextBox().Order(NextOrder()).AddClass("select").AddProperty("data-url", Urls.SelectPage);

            ViewConfig(m => m.ItemLength).AsTextBox().Order(NextOrder()).RegularExpression(RegularExpression.PositiveIntegers).DefaultValue = 44;

            ViewConfig(m => m.PopLink).AsTextBox().Order(NextOrder());

            ViewConfig(m => m.ListCode).AsTextBox().ReadOnly().Order(NextOrder()).AddClass("select").AddProperty("data-url", Urls.SelectList);

            ViewConfig(m => m.FormCode).AsTextBox().ReadOnly().Order(NextOrder());

            ViewConfig(m => m.ListId).AsHidden();

            ViewConfig(m => m.FormId).AsHidden();

            ViewConfig(m => m.AppId).AsHidden();

            ViewConfig(m => m.PartialView).AsDropDownList().Order(NextOrder()).DataSource(() =>
            {
                var path = (ServiceLocator.Current.GetInstance<IApplicationContext>() as CMSApplicationContext).MapPath("~/Modules/List/Views");
                Dictionary<string, string> templates = new Dictionary<string, string>();
                Directory.GetFiles(path, "Widget.ListTops*.cshtml").Each(f =>
                {
                    string fileName = Path.GetFileNameWithoutExtension(f);
                    templates.Add(fileName, fileName.Replace("Widget.", ""));
                });
                return templates;
            });
        }
    }

}